package middleware

import (
	"fmt"

	"github.com/gin-gonic/gin"
)

// LogMiddleWare 日志打印
func LogMiddleWare(c *gin.Context) {
	fmt.Println("LogMiddleWare start")
	fmt.Printf("完整请求路径是：%s\r\n", c.FullPath())
	fmt.Println("LogMiddleWare end")
}

// AuthMiddleware 鉴权
func AuthMiddleware(c *gin.Context) {
	fmt.Println("AuthMiddleware start")

	//获取token并解析
	token := c.GetHeader("Authorization")
	fmt.Println("token is:", token)

	if token == "" {
		c.JSON(200, gin.H{
			"code":    401,
			"message": "认证失败",
			"data":    "",
		})
		c.Abort()
	} else {
		//使用Set和Get来实现对数据的设置与接收
		c.Set("userId", "fengxu")
		c.Next()
	}
	fmt.Println("AuthMiddleware end")
}
